package com.receiver;

import com.receiver.database.DatabaseHelper;
import com.receiver.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ReceiverActivity extends Activity {
    /** Called when the activity is first created. */
	EditText userNameView = null;
	EditText passwordView = null;
	Button loginButton = null;
	Button createDbButton = null;
	Button insertButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userNameView = (EditText)findViewById(R.id.userNameView);
        passwordView = (EditText)findViewById(R.id.passwordView);
        loginButton = (Button)findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new DatabaseListener());
        createDbButton = (Button)findViewById(R.id.createDbButton);
        createDbButton.setOnClickListener(new CreateDatabaseListener());
    }
    
    public class DatabaseListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String editUserName = userNameView.getText().toString();
			String editPassword = passwordView.getText().toString();
			DatabaseHelper dbHelper = new DatabaseHelper(ReceiverActivity.this,"receiver_db",2);
			//SQLiteDatabase db = dbHelper.getWritableDatabase();
			/*
			ContentValues values = new ContentValues(); 
			values.put("id", 1);
			values.put("name", "1");
			values.put("password", "1");
			db.insert("user", null, values);
			*/
			
			try{
				SQLiteDatabase db = dbHelper.getReadableDatabase();
				Cursor cursor = db.query("login_user", new String[]{"id","name","password"}, "id=?", new String[]{"1"}, null, null, null);
				cursor.moveToNext();
				String name = cursor.getString(cursor.getColumnIndex("name"));
				String password = cursor.getString(cursor.getColumnIndex("password"));
				System.out.println("query--->" + name);
				System.out.println("pass--->" + password);
				if(!editUserName.equals(name)){
					Toast.makeText(ReceiverActivity.this, "用户名不存在，请重新填写用户名", Toast.LENGTH_SHORT).show();
				}
				else if(!editPassword.equals(password)){
					Toast.makeText(ReceiverActivity.this, "密码错误，请重新填写密码", Toast.LENGTH_SHORT).show();
				}
				else{
					Intent intent = new Intent();
					intent.setClass(ReceiverActivity.this, HistoryListActivity.class);
					startActivity(intent);
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}

		}
    }
	public class CreateDatabaseListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			DatabaseHelper dbHelper = new DatabaseHelper(ReceiverActivity.this, "receiver_db");
			
			try{	
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues(); 
				values.put("id", 1);
				values.put("name", "1");
				values.put("password", "1");
				db.insert("login_user", null, values);
			}
			catch(Exception e){
				e.printStackTrace();
			}
			}
    }
}